<?php
class UsersController extends AppController {
	public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow(array('login','register', 'facebook_login'));
    }
	public function login()
	{
		if ($this->request->is('post') && !empty($this->data['User'])) 
		{
			$pass = Security::hash(Configure::read('Security.salt') . $this->data['User']['password']);
			$data = $this->User-find('first',array(
				'conditions' => array(
					'username' => $this->data['User']['username'],
					'password' => $pass
				),
			));
		}
		
		if($this->Session->check('Facebook'))
		{
			$fb = $this->Session->read('Facebook');
		}
	}

	
	public function facebook_login()
	{
		if(!$this->Auth->user('id'))
		{
			if(!$this->Session->check('Facebook'))
			{
				$this->Redirect(FB::getLoginUrl(array('perms' => ConstFacebookApp::Scope)));
			}
			else {
				$fb = $this->Session->read('Facebook');
				if(!empty($fb))
				{
					$data = $this->User->find('first',array(
						'conditions'=> array(
							'facebook_id' => $fb['id']
						),
						'recursive' => -1
					));	
					if(!empty($data))
					{
						$this->Auth->login($data['User']);
					}
					else {
						$this->Redirect(array(
							'controller'=> 'users',
							'action'=> 'register'
						));
					}
				}
			}
		}
		
		$this->Redirect(Router::url('/', true));
	}
	
	public function register()
	{
		if ($this->request->is('post') && !empty($this->data['User'])) {
			$this->User->set($this->data);
			prr($this->data);
			
			if($this->User->saveAll($this->data, array('validate'=>'only')))
			{
				$user_data = array();
				
				if($this->Session->check('Facebook'))
				{
					$fb = $this->Session->read('Facebook');
					$user_data['User']['fb_id'] = $fb['id'];
				}
				
				// $user_data['User']['name'] = $this->data['User']['name']; 
				// 			$user_data['User']['password'] = Security::hash(Configure::read('Security.salt') . $this->data['User']['password']);
				// 			$user_data['User']['role'] = 'user';
				
				
				$user_data['User']['name'] = $this->data['User']['name'];
				$user_data['User']['email'] = $this->data['User']['email'];
				
				$user_data['User']['dob'] = $this->data['User']['dob'];
			
				$this->User->create();
				if($this->User->save($user_data['User']))
				{
					//save success, do something
					// $user_data['Profile']['user_id'] = $this->User->id;
					// 					$this->User->Profile->create();
					// 					prr($user_data);
					// 					if($this->User->Profile->save($user_data['Profile']))
					// 					{
					// 						$newUserData = $this->User->find('first',array(
					// 							'conditions' => array(
					// 								'User.id' => $this->User->id
					// 							),
					// 							'recursive' => -1
					// 						));
					// 						if(!empty($newUserData))
					// 						{
					// 							$this->Session->setFlash(__('The user has been saved'));
					// 							
					// 							$this->Auth->login($newUserData['User']);
					// 							return $this->redirect(array('controller' => 'home','action' => 'index'));	
					// 						}
					// 					}
									}
					$this->Session->setFlash(__('The user saved. Go next'));
			}
			else
			{
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		else {
			$data = array();
				if($this->Session->check('Facebook'))
				{
					$fbUser = $this->Session->read('Facebook');
					$data['User']['username'] = $fbUser['email'];
					$data['Profile']['name'] = $fbUser['name'];
				}
				
				$this->data = $data;
		}
	}
	
	
	
	
}
